import { $AuthorizeStatus } from '@/utils/const/env'
import { $api } from '@/utils';
export class Amap {
    private ready: boolean = false;
    constructor(callback?: Function) {
        const appAuthorizeSetting = uni.getAppAuthorizeSetting()
        if (appAuthorizeSetting) {
            switch (appAuthorizeSetting.locationAuthorized) {
                case $AuthorizeStatus.AUTHORIZED:
                    this.ready = true;
                    typeof callback === 'function' && callback();
                    break;
                case $AuthorizeStatus.DENIED:
                case $AuthorizeStatus.NOT_DETERMINED:
                    uni.getLocation({
                        type: 'gcj02',
                        altitude:false,
                        geocode:false,
                        isHighAccuracy:false,
                        success: (res:any)=> {
                            this.ready = true;
                            typeof callback === 'function' && callback();
                        },
                        fail: (err:any)=> {
                            console.log(err)
                            uni.showModal({
                                title: '提示',
                                content: '请先开启“定位权限”',
                                success: (res:any)=> {
                                    if (res.confirm) {
                                        uni.openAppAuthorizeSetting({
                                            success:()=>{}
                                        })
                                    }
                                }
                            })
                        }
                    })
                    break;
                case $AuthorizeStatus.CONFIG_ERROR:
                    break;
            }
        }
    }
    isReady() {
        return this.ready;
    }
    createMap(id: string) {
        return new Promise((resolve, reject) => {
            resolve(true);
        });
    }
    createMarker(map: any, lnglat: any) {
        return new Promise((resolve, reject) => {
            return reject('map is not ready');
        });
    }
    getCurrentPosition(option?:any) {
        const options = {
            type: 'gcj02',
            altitude:false,
            geocode:true,
            isHighAccuracy:true,
            ...option
        };
        return new Promise((resolve, reject) => {
            uni.getLocation({
                ...options,
                success: (res:any)=> {
                    resolve(res);
                },
                fail: (err:any)=> {
                    reject();
                }
            })
        });
    }
    getPosition(address: any) {
        return new Promise((resolve, reject) => {
            return reject('map is not ready');
        });
    }
    getAddress(lnglat: any) {
        return new Promise((resolve, reject) => {
            $api.Map.regeo({
                lnglat
            }).then((res:any)=> {
                let data = res.data;
                const formattedAddress = data.formatted_address;
                const addressComponent = data.addressComponent;
                data.desc = formattedAddress
                    .replace(addressComponent.province + addressComponent.city + addressComponent.district + addressComponent.township, '');
                resolve(data);
            }).catch((err:any)=> {
                reject(err);
            })
        });
    }
}